<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
    <%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
    <meta name="renderer" content="webkit">
    <%@ include file="top_new.jsp"%> 
</head>
<body>
  <div class="panel admin-panel">
  <div class="padding border-bottom layui-form-item">
    <div class="layui-input-inline">
      <input type="text" name="fvalue" id = "fvalue" placeholder="昵称" autocomplete="off" class="layui-input">
    </div>
    <button class="layui-btn" id ="query">查询</button>
    <!-- button class="layui-btn" id ="bt_add">添加</button> -->
  </div>
<table class="layui-table">
  <colgroup>
    <col width="30">
    <col width="100">
    <col width="30">
    <col width="60">
    <col width="60">
    <col width="100">
    <col width="200">
  </colgroup>
  <thead>
    <tr>
      <th name ="fid" id="fid">#</th>
      <th name ="fpage" id="fpage">网站名称</th>
      <th name ="fcycle" id="fcycle">执行周期</th>
      <th name ="fstats" id="fstats">抓取状态</th>
      <th name ="ftype" id="ftype">爬虫类型</th>
      <th >操作</th>
    </tr> 
  </thead>
  <tbody id = "t_tbody">
  </tbody>
    <tr>
       <td colspan="5"><div id="div_page"></div></td>
       <td colspan="">记录数:40</td>
    </tr>
</table>  
  </div>
<script type="text/javascript"> 
window.onload=function() {
	$.post("<%=basePath%>magic/magic_list",
		    {
				page:1,
				pagesize:10
		    },
		    function(res){
		    	if (res.errorCode == 0) {
		    		var data = res.data;
		    		laypage({
		    		      cont: 'div_page', //容器。值支持id名、原生dom对象，jquery对象。【如该容器为】：<div id="page1"></div>
		    		      pages: data.totalPage, //通过后台拿到的总页数
		    		      curr: data.pageNumber, //当前页
		    		      jump: function(obj, first){ //触发分页后的回调
		    		    	  $("#t_tbody").empty();
		    		    	  if(!first){
			    		    	  $.post("<%=basePath%>magic/magic_list",
			    		    			    {
			    		    					page:obj.curr,
			    		    					pagesize:10,
			    		    					fvalue:$("#fvalue").val()
			    		    			    },
			    		    			    function(res){
			    		    			    	var tbody ="";
		    		    			    		if (res.errorCode == 0) {
			    		    			    		 tbody = pageDataMagic(res.data.list,0);  
		    		    			    		}
		    		    			    		$("#t_tbody").append(tbody);  
			    		    			    });
		    		    	  }else{
		    		    		   var tbody ="";
		    		    		   tbody = pageDataMagic(data.list,0);  
		    			    	   $("#t_tbody").append(tbody);  
		    		    	  }
		    		    	 
		    		      }
		    		});
		    	}else{
		    		if (res.errorCode == 2) {
		    			layer.alert(res.message) ;
		    		}else{
		    			layer.msg("参数错误："+res.message, {shift: 6});
		    		}
		    	}
		    }); 
};
$("#query").click(function(){ 
	 var fvalue = $("#fvalue").val();
	 $.post("<%=basePath%>magic/magic_list",
			    {
					page:1,
					pagesize:10,
					fvalue:fvalue
			    },
			    function(res){
			    	if (res.errorCode == 0) {
			    		var data=res.data;
			    		laypage({
			    		      cont: 'div_page', //容器。值支持id名、原生dom对象，jquery对象。【如该容器为】：<div id="page1"></div>
			    		      pages: data.totalPage, //通过后台拿到的总页数
			    		      curr: data.pageNumber, //当前页
			    		      jump: function(obj, first){ //触发分页后的回调
			    		    	  $("#t_tbody").empty();
			    		    	  $.post("<%=basePath%>magic/magic_list",
			    		    			    {
			    		    					page:obj.curr,
			    		    					pagesize:10,
			    		    					fvalue:fvalue
			    		    			    },
			    		    			    function(res){
		    		    			    		if (res.errorCode == 0) {
		    		    			    			var tbody ="";
			    		    			    		if (res.errorCode == 0) {
				    		    			    		 tbody = pageDataMagic(res.data.list,0);  
			    		    			    		}
			    		    			    		$("#t_tbody").append(tbody);   
		    		    			    		}
			    		    			    });
			    		      }
			    		});
			    	}else{
			    		if (res.errorCode == 2) {
			    			layer.alert(res.message) ;
			    		}else{
			    			layer.msg("参数错误："+res.message, {shift: 6});
			    		}
			    	}
			    }); 
})

/**
 * 开启抓取
 */
function Wstats(bt,num,state){
	var id = $(bt).attr("id");
	var stateid = num.id;
	if(state == "1"){
		layer.msg("此状态已为开启", {shift: 6});
		return false;
	}
	$.post("<%=basePath%>magic/magic_work",
		    {
				fid:id,
				fstats:1
		    },
		    function(ret){
		    	if (ret.isOk) {
		    		//$(bt).attr('disabled',"disabled");
		    		$(bt).text("暂停");	
		    		var event = 'stop(this,'+stateid+',1)';
		    		$(bt).attr("onclick",event);
		    		var event3 = 'remove(this,'+stateid+',1)'
		    		$(bt).next().attr('onclick',event3);
		    		$(num).html("爬取中"); 
		    		//使用 setTimeout 时需注意，当该代码执行时，JS 会立即编译函数第一个参数“code”
		    		//所以该函数的第一个参数应该为：需要编译的代码、或者一个函数
		    		//例1：setTimeout("alert('x')", 2000);
		    		//例2：setTimeout(function () { alert('x'); }, 2000);
		    		 
		    		//错误示例：setTimeout(alert('x'), 2000); "x" 会立马跳出来，延时没有效果
		    		//setTimeout(function () { 
		    		//	$(bt).removeAttr("disabled");
		    		//	$(bt).text("开始");
		    	  	//}, 8000);
		    	}else{
		    		layer.msg("未知错误,请联系管理员!", {shift: 6});
		    	}
		    }); 
}

/**
 * 暂停抓取
 */
function stop(bt,num,state){
	var id = $(bt).attr("id");
	var stateid = num.id;
	if(state == "2"){
		layer.msg("此状态已为暂停", {shift: 6});
		return false;
	}else{
		$.post("<%=basePath%>magic/magic_stop", 
				{
					fid:id
				}, 
				function(ret){
					if(ret.isOk){
						//按钮设为开始
						//状态设为‘开始’
						//将事件改为开启事件
			    		$(bt).text("开启");
			    		$(num).html("已暂停");
			    		var event = 'Wstats(this,'+stateid+',2)';
			    		$(bt).attr('onclick',event);
			    		var event3 = 'remove(this,'+stateid+',2)'
			    		$(bt).next().attr('onclick',event3);
					}else{
						layer.msg("未知错误,请联系管理员!", {shift: 6});
					}
				}
		);
	}
}

/**
 * 移除
 */
function remove(bt,num,state){
	var id = $(bt).attr("id");
	var stateid = num.id;
	if(state == "0"){
		layer.msg("此状态已为移除", {shift: 6});
		return false;
	}else{
		$.post("<%=basePath%>magic/magic_remove", 
				{
					fid:id
				}, 
				function(ret){
					if(ret.isOk){
						//按钮设为开始
						//状态设为‘开始’
						//将事件改为开启事件
			    		$(bt).text("移除");
			    		$(num).html("已移除");
			    		var event = 'remove(this,'+stateid+',0)'; 
			    		$(bt).attr('onclick',event);
					}else{
						layer.msg("未知错误,请联系管理员!", {shift: 6});
					}
				}
		);
	}
}

/**
 * 立即执行
 */
function startnow(bt,num){
	var id = $(bt).attr("id");
	var stateid = num.id;
	if(stateid == ""){
		layer.msg("参数错误", {shift: 6});
		return false;
	}else{
		$.post("<%=basePath%>magic/magic_startnow", 
				{
					fid:id
				}, 
				function(ret){
					if(ret.isOk){
						//按钮设为开始
						//状态设为‘开始’
						//将事件改为开启事件
						$(bt).attr('disabled',"disabled");
			    		$(bt).text("执行中");
			    		$(num).html("爬取中");
			    		setTimeout(function () { 
			    			layer.msg("执行完毕，请查看日志", {shift: 2});
				    		$(bt).removeAttr("disabled");
				    		$(bt).prev().prev().text("开启"); 
				    		$(bt).text("立即执行");
				    		$(num).html("已移除");
				    	  	}, 4000);
					}else{
						layer.msg("未知错误,请联系管理员!", {shift: 6});
					}
				}
		);
	}
}
 
function magiclog(id){
	   var diag = new top.Dialog();
	    diag.Drag=true;
	    diag.Title ="爬虫日志";
	    diag.URL = '<%=basePath%>magic/magic_log?fid='+id;
	    diag.Width = 500;
	    diag.Height = 550;
	    diag.CancelEvent = function(){ //关闭事件
	        diag.close();
	    };
	    diag.show();
}

function magiclog(id){
	   var diag = new top.Dialog();
	    diag.Drag=true;
	    diag.Title ="爬虫日志";
	    diag.URL = '<%=basePath%>magic/magic_log?fid='+id;
	    diag.Width = 500;
	    diag.Height = 550;
	    diag.CancelEvent = function(){ //关闭事件
	        diag.close();
	    };
	    diag.show();
}
function magic_grid(id){
	   var diag = new top.Dialog();
	    diag.Drag=true;
	    diag.Title ="爬虫数据";
	    diag.URL = '<%=basePath%>magic/magic_grid?fid='+id;
	    diag.Width = 700;
	    diag.Height = 550;
	    diag.CancelEvent = function(){ //关闭事件
	        diag.close();
	    };
	    diag.show();
}
function Winfo(id){
	var diag = new top.Dialog();
    diag.Drag=true;
    diag.Title ="爬虫详情";
    diag.URL = '<%=basePath%>magic/magic_edit?fid='+id;
    diag.Width = 450;
    diag.Height = 550;
    diag.CancelEvent = function(){ //关闭事件
    	var isLoad = diag.innerFrame.contentWindow.document.getElementById("isLoad").value;
    	if(isLoad == '1'){
    		$("#query").click();
    	}
        diag.close();
    };
    diag.show();
}
</script>
</body></html>